public class SlidingWindow {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for (int i : nums) {
            sum += i;
        }
        if (sum - x < 0) {
            return -1;
        }
        if (sum - x == 0) {
            return nums.length;
        }
        int n = nums.length;
        int count = n + 1;
        for (int left = 0, right = 0, sum1 = 0; right < n; right++) {
            sum1 += nums[right];
            while (left <= right && sum1 > sum - x) {
                sum1 -= nums[left++];
            }
            if (sum1 == sum - x) {
                count = Math.min(count, n - (right - left + 1));
            }
        }
        return count == n + 1 ? -1 : count;
    }
}
